package filesearcher;

import java.io.*;
import java.util.*;

public class FileSearcher {
    HashMap<String, Integer> words = new HashMap<>();
    FileSearcher (String file_path) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file_path))));

            String line = null;
            while ((line = reader.readLine()) != null) {
                line = line.replaceAll("[^a-zA-Z]", " ");
                line = line.toLowerCase();
                Scanner lineScan = new Scanner(line);
                while (lineScan.hasNext()) {
                    String word = lineScan.next();
                    if (words.computeIfPresent(word, (key, value) -> value + 1) == null) {
                        words.put(word, 1);
                    }
                }
            }
        }
        catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException");
        }
        catch (IOException e) {
            System.out.println("IOException");
        }
    }
    Integer search (String word) {
        return words.getOrDefault(word, 0);
    }
}
